package main

import (
	"fmt"
	"strings"
)

/*
创建: make(mapp[string]int)
获取元素: m[key]
key不存在的时候，获取value类型的初始值
用value,ok := m[key]来判断是否存在key
遍历：
	使用range遍历key,或者遍历key,value对
	不保证遍历顺序，如需顺序，需手动对key排序
map使用哈希表，必须可以比较相等
除了slice，map,function的内建类型都可以作为key
Struct类型不包含上述字段，也可以作为key
*/
func main() {
	//map1()
	//rangeMap()
	//getValuesFromMap()
	//deleteValueFromMap()
	// 测试map
	//fmt.Println(lengthOfNonRepeatingSubStr("abcbatd"))
	//fmt.Println(lengthOfNonRepeatingSubStr(""))
	//fmt.Println(lengthOfNonRepeatingSubStr("bbbb"))
	//字符串操作
	strUtils()
}

func map1() {
	m := map[string]string{
		"name":    "ccmouse",
		"course":  "golang",
		"site":    "imooc",
		"quality": "nobad",
	}
	m2 := make(map[string]int) // m2 == empty map
	var m3 map[string]int      //m3 == nil
	fmt.Print(m, m2, m3)
}

func rangeMap() {
	m := map[string]string{
		"name": "yuanpengyue",
		"age":  "23",
	}
	for k, v := range m {
		fmt.Println(k, v)
	}
}

func getValuesFromMap() {
	m := map[string]string{
		"name": "yuanpengyue",
		"age":  "23",
	}
	if name, ok := m["neme"]; ok {
		fmt.Println(name, ok)
	} else {
		fmt.Println("key does not exist")
	}
}

func deleteValueFromMap() {
	m := map[string]string{
		"name": "yuanpengyue",
		"age":  "23",
	}

	delete(m, "name")
	name, ok := m["name"]
	fmt.Println(name, ok)
}

func lengthOfNonRepeatingSubStr(s string) int {
	lastOccurred := make(map[byte]int)
	start := 0
	maxLength := 0
	for i, ch := range []byte(s) {
		if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
			start = lastI + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastOccurred[ch] = i
	}
	return maxLength
}

// 使用utf8.RuneCountInString获得字符数量
func strUtils() {
	//去除前后空格
	fields := strings.Fields("   13 3 ")
	fmt.Println(fields)
	// join,Split

	//contains,index
	//tolower,toupper
	//trim,trimRight,trimLeft
}
